$(function() {
	$("#manage").datagrid({
		url: APP + "/Admin/Manage/getManage",
		fit: true,
		fitColums: true,
		striped: true,
		rownumbers: true,
		border: false,
		pagination: true,
		pageSize: 20,
		pageList: [10, 20, 30, 40, 50],
		pageNumber: 1,
		sortName: 'date',
		sortOrder: 'desc',
		toolbar: "#manage_tool",
		columns: [
			[{
				field: 'member_id',
				title: '自动编号',
				width: '10%',
				checkbox: true
			}, {
				field: 'username',
				title: '用户名',
				width: '20%'
			}, {
				field: 'email',
				title: '邮箱',
				width: '20%'
			}, {
				field: 'phone',
				title: '手机号',
				width: '20%'
			}, {
				field: 'auth',
				title: '管理权限',
				width: '20%'
			}, {
				field: 'create_time',
				title: '创建时间',
				width: '10%'
			}]
		]

	})

	//添加
	$("#manage_add").dialog({
		title: '添加表单',
		width: 350,
		modal: true,
		closed: true,
		buttons: [{
			text: '提交',
			iconCls: 'icon-add',
			handler: function() {
				if ($("#manage_add").form('validate')) {
					$.ajax({
						type: "post",
						url: APP + "/Admin/Manage/addManage",
						data: {
							"username": $("#manage_add input[name=username]").val(),
							"password": $("#manage_add input[name = password]").val(),
							"email": $("#manage_add input[name=email]").val(),
							"phone": $("#manage_add input[name=phone]").val(),
							"auth": $(".combo").find('input').val()
						},
						dataType: 'json',
						beforeSend: function() {
							$.messager.progress({
								text: "正在提交.."
							})
						},
						success: function(data, response, status) {
							$.messager.progress('close');
							if (data) {
								$.messager.show({
									title: '提示',
									msg: '添加成功'
								});
								$('#manage_add').dialog('close').form('reset');
								$('#manage').datagrid('reload');
							} else {
								$.messager.alert("失败信息", "添加失败");
							}
						}
					});
				}
			}
		}, {
			text: '取消',
			iconCls: 'icon-cancel',
			handler: function() {
				$("#manage_add").dialog('close').form('reset');
			}
		}]
	})

	//编辑
	$("#manage_edit").dialog({
		title: '编辑表单',
		width: 350,
		modal: true,
		closed: true,
		buttons: [{
			text: '提交',
			iconCls: 'icon-page_edit',
			handler: function() {
				$.ajax({
					type: "post",
					url: APP + "/Admin/Manage/updateManage",
					data: {
						"id" : $("#manage_edit input[name=member_id]").val(),
						"username": $("#manage_edit input[name=username]").val(),
						"password": $("#manage_edit input[name = password]").val(),
						"email": $("#manage_edit input[name=email]").val(),
						"phone": $("#manage_edit input[name=phone]").val(),
						"auth": $("#manage_edit .combo").find('input').val()
					},
					dataType: 'json',
					beforeSend: function() {
						$.messager.progress({
							text: "正在修改.."
						})
					},
					success: function(data, response, status) {
						$.messager.progress('close');
						
						if (data) {
							$.messager.show({
								title: '提示',
								msg: '修改成功'
							});
							$('#manage_edit').dialog('close').form('reset');
							$('#manage').datagrid('reload');
						} else {
							$.messager.alert("失败信息", "添加失败");
						}
					}
				});
			}
		}, {
			text: '取消',
			iconCls: 'icon-cancel',
			handler: function() {
				$("#manage_edit").dialog('close').form('reset');
			}
		}]
	});
	
	
	//删除
	
	
	

	//验证账号
	$("input[name=username]").validatebox({
			required: true,
			validType: "length[2,20]",
			missingMessage: "请输入管理员账号",
			invalidMessage: "管理员账号不能为空",
		})
		//验证密码
	$("input[name=password]").validatebox({
			required: true,
			validType: "length[6,30]",
			missingMessage: "请输入管理员密码",
			invalidMessage: "管理员密码不能为空",
		})
		//验证邮箱
	$("input[name=email]").validatebox({
			required: true,
			validType: "email",
			missingMessage: "请输入管理员邮箱",
			invalidMessage: "管理员邮箱不能为空",
		})
		//验证手机
	$("input[name=phone]").validatebox({
			required: true,
			validType: "",
			missingMessage: "请输入管理员手机",
			invalidMessage: "管理员手机不能为空",
		})
		//权限
	$("#manage_add input[name=auth]").combotree({
		url: APP + '/Admin/Index/navData',
		lines: true,
		required: true,
		multiple: true,
		checkbox: true,
		onlyLeafCheck: true,
		onLoadSuccess: function(node, data) {
			var _this = this;
			if (data) {
				$(data).each(function(index, value) {
					if (this.state == 'closed') {
						$(_this).tree('expandAll');
					}
				})
			}
		}
	})

	//配置
	manage_bool = {
		add: function() {
			$("#manage_add").dialog('open');
			$("#manage_add input[name=username]").select();
		},
		edit: function() {
			var rows = $("#manage").datagrid('getSelections');
			if (rows.length == 0) {
				$.messager.alert("警告操作", '至少选择一条编辑', 'warning');
			} else if (rows.length == 1) {
				$.ajax({
					type: "post",
					url: APP + "/Admin/Manage/editManage",
					data: {
						'id': rows[0].member_id
					},
					dataType: 'json',
					beforeSend: function() {
						$.messager.progress({
							text: "获取中.."
						})
					},
					success: function(data, response, status) {
						$.messager.progress('close');
						
						if (data) {
							var auth = data.auth.split(',');
							
							$('#manage_edit').form('load', {
								member_id: data.member_id,
								username: data.username,
								email: data.email,
								phone: data.phone,
								//								auth : data.auth
							}).dialog('open');

							//分配权限
							$("#manage_edit input[name=auth]").combotree({
								url: APP + '/Admin/Index/navData',
								lines: true,
								required: true,
								multiple: true,
								checkbox: true,
								onlyLeafCheck: true,
								onLoadSuccess: function(node, data) {
									var _this = this;
									if (data) {
										$(data).each(function(index, value) {
//											console.log(value);
											if($.inArray(value.text,auth) != -1){
												$(_this).tree('check',value.target);
											}
											
											if (this.state == 'closed') {
												$(_this).tree('expandAll');
											}
										})
									}
								}

							})

						} else {
							$.messager.alert("失败信息", "获取失败");
						}
					}

				});


			} else {
				$.messager.alert("警告操作", '只能选择一条编辑', 'warning');
			}
		},
		del:function(){//删除
			var rows = $("#manage").datagrid('getSelections');
			if(rows.length  == 0){
				$.messager.alert("警告操作", '请先选择一条数据', 'warning');
			}else{
				$.each(rows,function(i,v){
					$.ajax({
						type:"post",
						url:APP+"/Admin/Manage/delManage",
						data : {'id':v.member_id},
						dataType: 'json',
						beforeSend:function(){
							$.messager.progress({
								text: "获取中.."
							});
						},
						success : function(data, response, status){
							$.messager.progress('close');
							$.messager.show({
								title: '提示',
								msg: '修改成功'
							});
							$('#manage').datagrid('reload');
						}
					});
				})
			}
		},
		reload:function(){
			$('#manage').datagrid('reload');
		},
		unselect : function(){
			$('#manage').datagrid('unselectAll');
		}
		
		
		
		
	}




})